package cn.information.view.extend;

import cn.information.bean.Supplier;
import cn.information.dao.SupplierDao;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Vector;

public class SupplierQueryUI extends JFrame {
    private JComboBox<Supplier> supplierComboBox;
    private JButton btnConfirm;

    public SupplierQueryUI() {
        setTitle("供应商查询");
        setSize(400, 200);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        JPanel panelCenter = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20));
        JPanel panelSouth = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));

        // 获取所有供应商
        SupplierDao supplierDao = new SupplierDao();
        List<Supplier> suppliers = SupplierDao.findAll();

        panelCenter.add(new JLabel("请选择供应商:"));
        supplierComboBox = new JComboBox<>(new Vector<>(suppliers));
        panelCenter.add(supplierComboBox);

        btnConfirm = new JButton("确认");
        btnConfirm.setEnabled(false); // 初始不可用
        panelSouth.add(btnConfirm);

        JButton btnCancel = new JButton("取消");
        btnCancel.addActionListener(e -> dispose());
        panelSouth.add(btnCancel);

        add(panelCenter, BorderLayout.CENTER);
        add(panelSouth, BorderLayout.SOUTH);

        supplierComboBox.addActionListener(e -> {
            if (supplierComboBox.getSelectedItem() != null) {
                btnConfirm.setEnabled(true);
            } else {
                btnConfirm.setEnabled(false);
            }
        });

        btnConfirm.addActionListener(e -> {
            Supplier selectedSupplier = (Supplier) supplierComboBox.getSelectedItem();
            new SupplierQueryResultUI(selectedSupplier).setVisible(true);
            dispose();
        });

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}